import WxValidate from "../../../utils/WxValidate.js";
import tool from "../../../utils/tool.js";
const app = getApp();
var url = app.getURL();

Page({
  data: {
    min: 0,  // 最小限制 
    max:10,   // 最大限制
    weight:0, // 当前value
    // 默认值
    totalPrice:0,
    form:{},
    hidden:true,
    picker: ['顺丰快递', '京东快递','韵达快递','中通快递','圆通快递','申通快递','邮政快递','百世快递'],
  },
// 拖动过程中触发的事件
sliderchanging(e){
  var weight = e.detail.value;
  this.setData({ weight: weight })
},
// 完成一次拖动后触发的事件
sliderchange(e){
  var weight = e.detail.value;
  this.setData({ weight: weight })

  // 计算价钱
  if(weight<=3){
    this.setData({ totalPrice:3})
  }
  else if(weight>3&&weight<=8){
      this.setData({ totalPrice: weight})
  } 
  else this.setData({ totalPrice:12})
},
  
// 单选改变值
PickerChange(e) {
   this.setData({
     index: e.detail.value,
   })
 },
  hideKeyboard:function(e){
    if(e.detail.value.length>=11){
      wx.hideKeyboard()
  }
},
 
// 初始化表单验证
onLoad: function () {
  // this.isLogin();
  this.initValidate();
  let that = this;
  //获取基本信息
  wx.getStorage({
    key: 'allMessage',
    success:function(res){
      that.setData({
        message: res.data
      })
    }
  })
},

//判断是否登录，如果未登录的话就提示登录
isLogin(){
  let that = this;
//获取本地缓存的用户信息
let userName = wx.getStorageSync('cookieNickname')
  if(userName==null||userName==''){
    wx.showToast({
      title: "请先登录",
      icon: "none",
      duration: 2000
      })
      setTimeout(function(){
        wx.switchTab({
          url: '../../my/user/user'
        });
      },600)  
  }
},
// 报错
showModal(error) {
  wx.showModal({
    content: error.msg,
    showCancel: false,
  })
},

initValidate() {
  const rules = {
    name: {
      required: true,
      maxlength: 10
    },
    phone: {
      required: true,
      tel: true
    },
    address:{
      required: true
    },
    company: {
      required: true,
    }, 
    weight: {
      required: true,
    },
    code:{
      required: true,
      minlength: 4
    }
  }

  const message = {
    name: {
      required: '请输入姓名',
      maxlength: '名字不能超过10个字'
    },
    phone: {
      required:'请填写手机号',
      tel:'请填写正确的手机号'
    }, 
    address: {
      required: '请填写宿舍地址'
    }, 
    company: {
      required: '请选择快递公司',
    }, 
    weight: {
      required: '请选择快递重量',
    },
    code: {
      required: '请输入取件码',
      minlength: '请填写正确的取件码'
    }
  }
  //实例化当前的验证规则和提示消息
  this.WxValidate = new WxValidate(rules, message);
},
formSubmit: tool.throttle(function (e){
  this.submit(e);
},8000),
submit: function(e) {
  let that = this
  // console.log('传递后form发生了submit事件，携带的数据为：', e.detail)
  const params = e.detail.value

  //校验表单
  if (!this.WxValidate.checkForm(params)) {
    const error = this.WxValidate.errorList[0]
    this.showModal(error)
    return false
  }
  else {
  wx.request({
    url: url+ '/order/app/order/bussi/express/auth/submitExpressBussi',
    data: {
      "custId": this.data.message.id,
      "address": params.address,
      "company": params.company,
      "expressCode": params.code,
      "expressWeight": params.weight,
      "name": params.name,
      "phone": params.phone,
      "remark": params.remarks
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {// 设置请求的 header
      'content-type': 'application/json',
      token:wx.getStorageSync('cookieToken')
    },
    success: function (res) {
      // 验证通过后执行部分
      let orderNbr = res.data.data.orderNbr
      console.log(orderNbr)
      if(res.data.code==200){
        // 增加延时提交动画
        wx.showToast({
          title: '提交中',
          icon: 'loading',
          duration: 1000
        });
        setTimeout(function(){
          wx.showToast({
            title: '提交成功'
          })
        },1000)
        // 跳转支付
        that.goPay(orderNbr)
      }else{
        wx.showToast({
          title: '提交失败',
          icon: 'error',
          duration: 1000
        });
      }
      },
    })
    }
  },

  // 订单支付
  goPay(e){
    let payNbr=e
    wx.request({
      url: url+'/order/app/order/auth/payOrder',
      method: "POST",
      header: {// 设置请求的 header
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      data: { order:payNbr },
      success(res) {
        console.log(res)
        if(res.data.code==200){
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 1000
          })
          setTimeout(function(){
            wx.switchTab({
                url: '/pages/index/index'
            })  
        },1500)
        }else if(res.data.code==301){
          wx.showToast({
            title: '钱包未开通',
            icon: 'error',
            duration: 1000
          })
          setTimeout(function(){
            wx.redirectTo({
                url: '/pages/my/user/wallet/wallet'
            })  
        },1500)
        }else if(res.data.code==412){
          wx.showToast({
            title: '订单不存在',
            icon: 'error',
            duration: 1000
          })
          setTimeout(function(){
            wx.redirectTo({
                url: '/pages/my/user/wallet/wallet'
            })  
        },1500)
        }else if(res.data.code==413){
          wx.showToast({
            title: '订单支付失败',
            icon: 'error',
            duration: 1000
          })
          setTimeout(function(){
            wx.redirectTo({
                url: '/pages/my/user/wallet/wallet'
            })  
        },1500)
        }else{
          wx.showToast({
            title: '支付失败',
            icon: 'error',
            duration: 1000
          })
          setTimeout(function(){
            wx.redirectTo({
                url: '/pages/my/user/wallet/wallet'
            })  
          },1500)
        }
      }
    })
  },
})
